<template>
	<view class="order">
		<view class="top">
			<view class="top-title">
				<text>猫之巢</text>
			</view>
			<view class="top-notice">
				<image src="/static/image/notice.png" mode="widthFix"></image>
				<text>公告</text>
			</view>

		</view>
		<view class="order-content">
			<view class="">
			</view>
			<scroll-view class="order-left" scroll-y="true">
				<view v-for="(items,key) in classificationData"
					:class="'order-left-item '+(selectIndex==key?'active':'')" @click="select(key)">
					<text>{{items.name}}</text>
				</view>
			</scroll-view>
			<scroll-view class="order-right" scroll-y="ture" style="height: 900rpx;">
				<text class="order-title">{{classificationData[selectIndex].name}}</text>
				<view class="order-right-item" v-for="item in goodsData">
					<view class="image">
						<image :src="'http://localhost:8088/file/downloadGoods?t='+item.image" mode=""></image>
					</view>
					<view class="msg">
						<view class="msg-left" @click="toDetail(item.id)">
							<text class="name">{{item.name}}</text>
							<text class="introduce">{{item.description}}</text>
							<text class="price">￥{{(item.price/100).toFixed(2)}}</text>
						</view>
						<view class="msg-right">
							<button @click="buy(item)">购</button>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<uni-popup ref="popup" type="bottom">
			<view class="cart-msg">
				<view class="cart-msg-top">
					<text>购物车</text>
					<image src="../../static/image/x.png" mode="widthFix" @click="close"></image>
				</view>
				<view class="nomal" v-show="cartMsg.length==0">
					<text>
						购物车空空如也
					</text>
				</view>
				<scroll-view class="cart-msg-content" scroll-y="true">
					<view class="cart-item" v-for="item in cartMsg">
						<view class="image" @click="toDetail(item.goodId)">
							<image :src="'http://localhost:8088/file/downloadGoods?t='+item.image" mode=""></image>
						</view>
						<view class="center" @click="toDetail(item.goodId)">
							<text class="name">{{item.name}}</text>
							<text class="del">{{item.createTime}}</text>
							<text class="price">￥{{(item.amount/100).toFixed(2)}}</text>
						</view>
						<view class="cart-right">
							<uni-number-box :value="item.number" background="#e89abe" color="#fff"
								@change="cartChange(item,$event)" />
						</view>
					</view>
				</scroll-view>
			</view>
		</uni-popup>
		<view class="bottom">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				classificationData: [{}],
				selectIndex: 0,
				goodsData: [],
				// 底部数据
				options: [{
					icon: 'cart',
					text: '购物车',
					info: 0
				}, {
					icon: 'trash',
					text: '清空购物车',
					info: 0
				}],
				buttonGroup: [{
					text: '结账',
					backgroundColor: 'linear-gradient(90deg, #f6cce8, #e89abe)',
					color: '#fff'
				}],
				userId: '',
				cartMsg: [],
			}
		},
		onLoad() {
			this.select(0);
			this.userId = JSON.parse(window.localStorage.getItem('user'));
			this.getCart();
		},
		onShow() {
			this.getclassificationData();
			this.select(0);
			this.userId = JSON.parse(window.localStorage.getItem('user'));
			this.getCart();
		},
		methods: {
			close() {
				this.$refs.popup.close('bottom')
			},
			onClick(e) {
				if (e.index == 1) {
					uni.showModal({
						title: "确定清空购物车?",
						confirmColor: "#e89abe",
						success: (res) => {
							// 判断是否点击确定
							if (res.confirm) {
								uni.showLoading();
								uni.request({
									url: '/cart/removeCart',
									data: {
										userId: this.userId
									},
									success: (res) => {
										if (res.data.code == 400) {
											uni.showToast({
												title: "清空购物车失败！！",
												icon: "error"
											})
										} else {
											uni.showToast({
												title: "清空购物车！",
												icon: "success"
											})
										}
										uni.hideLoading();
										this.getCart();
									}
								})
							}
						}
					})
				} else {
					this.$refs.popup.open('bottom')
				}
			},
			buttonClick(e) {
				if (this.cartMsg.length == 0) {
					uni.showToast({
						title: "未选择商品!",
						icon: "error"
					});
					return;
				}
				uni.navigateTo({
					url: '/pages/buy/buy',
				})
			},
			getclassificationData() {
				uni.request({
					url: '/goods/getClassList/',
					data: {
						page: 1,
						pageSize: 10
					},
					dataType: 'json',
					method: 'GET',
					success: (res) => {
						this.classificationData = res.data.data;
						console.log("class分类", res.data.data);
						this.select(0);
					}
				})
			},
			select(key) {
				this.selectIndex = key;
				uni.showLoading({
					title: '加载中'
				})
				uni.request({
					url: '/goods/selectByclass/',
					data: {
						categoryId: this.classificationData[key].id
					},
					dataType: 'json',
					method: 'GET',
					success: (res) => {
						this.goodsData = res.data.data;
						console.log("class分类", res.data.data);
						uni.hideLoading();
					}
				})
			},
			toDetail(id) {

				uni.navigateTo({
					url: '/pages/details/details?id=' + id,
				})
			},
			buy(item) {
				console.log(item);
				console.log(this.userId);
				uni.request({
					url: '/cart/pushCart',
					method: 'POST',
					data: {
						name: item.name,
						image: item.image,
						userId: this.userId,
						goodId: item.id,
						classId: item.category_id,
						number: 1,
						amount: item.price,
					},
					success: (res) => {
						console.log(res);
						uni.showToast({
							title: res.data.data,
							icon: 'success'
						});
						this.getCart();
					}
				})
			},
			getCart() {
				uni.request({
					url: '/cart/getAllCart',
					data: {
						id: this.userId,
					},
					success: (res) => {
						console.log(res);
						this.cartMsg = res.data.data.records;
						let num = 0;
						for (var i = 0; i < this.cartMsg.length; i++) {
							num += this.cartMsg[i].number;
						}
						this.options[0].info = num;
					}
				})
			},
			cartChange(item, e) {
				uni.request({
					url: '/cart/changeCart',
					method: 'POST',
					data: {
						id: item.id,
						number: e
					},
					success: (res) => {
						console.log(res);
						this.getCart();
					}
				})
			}

		}
	}
</script>

<style lang="scss">
	.clearCart {
		color: gray;
		font-size: 24rpx;
		margin-left: 20rpx;
		margin-top: 6rpx;
	}

	.cart-item {
		width: 100%;
		height: 150rpx;
		border-bottom: 1px solid rgba(0, 0, 0, 0.1);
		background-color: white;
		display: flex;
		align-items: center;

		.cart-right {
			width: 50rpx;
			height: 50rpx;
			background-color: #e89abe;
			color: white;
			font-weight: 600;
			text-align: center;
			border-radius: 50%;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.image {
			width: 130rpx;
		}

		.image image {
			height: 120rpx;
			width: 120rpx;
			border-radius: 10rpx;
		}

		.center {
			margin-left: 20rpx;
			width: 400rpx;

			.del {
				font-size: 24rpx;
				color: dimgray;
				display: block;
			}
		}
	}

	.cart-item:last-child {
		border: none;
	}

	.cart-msg-content {
		box-sizing: border-box;
		width: 100%;
		height: 300rpx;
		padding: 10rpx 20rpx;
		margin-top: 20rpx;
	}

	.cart-msg {
		height: 600rpx;
		bottom: 200rpx;
		padding-top: 20rpx;
		background-color: #ffffff;
		width: 100%;
	}

	.cart-msg-top {
		font-size: 30rpx;
		margin: 5rpx 30rpx;
		display: flex;
		justify-content: space-between;
		color: gray;
	}

	.cart-msg-top image {
		width: 30rpx;
	}

	.top {
		padding: 30rpx;
	}

	.top-title {
		font-size: 40rpx;
		font-weight: 600;
	}

	.top-notice {
		color: gray;
		font-size: 24rpx;
		margin-top: 40rpx;
		display: flex;
		align-items: center;
	}

	.top-notice image {
		width: 24rpx;
		align-items: center;
	}

	.order-content {
		display: flex;
	}

	.order-right {
		.order-title {
			font-size: 24rpx;
			color: gray;
			margin-left: 40rpx;
		}
	}

	.order-left {
		flex: 1;
		height: 940rpx;
		background-color: #f6f6f6;
		display: inline-block;
	}

	.order-left-item {
		box-sizing: border-box;
		height: 120rpx;
		width: 200rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: gray;
	}

	.active {
		border-left: 5px solid #e89abe;
		background-color: white;
		color: black;
	}

	.order-right-item {
		height: 120rpx;
		display: flex;
		padding: 20rpx;
		justify-content: space-evenly;
		align-items: center;
	}

	.order-right-item image {
		height: 150rpx;
		width: 150rpx;
	}

	.msg {
		display: flex;
		justify-content: space-between;
	}

	.order-right-item .msg text {
		margin-left: 20rpx;
		display: block;
	}

	.order-right-item .msg .name {
		width: 200rpx;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		font-weight: 600;
	}

	.order-right-item .msg .introduce {
		color: gray;
		width: 200rpx;
		font-size: 24rpx;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.order-right-item .msg .price {
		text-align: left;
		font-weight: 600;
		width: 140rpx;
		color: #e89abe;
		text-shadow: 1rpx 1rpx 5rpx #e89abe;
	}

	.order-right-item .msg {
		align-items: center;
	}

	.order-right-item .msg button {
		align-items: center;
		font-size: 24rpx;
		height: 60rpx;
		display: block;
		float: right;
		background-color: #e89abe;
		color: white;
	}

	.nomal {
		color: gray;
		font-size: 24rpx;
		text-align: center;
	}

	.bottom {
		position: fixed;
		bottom: 100rpx;
		left: 0;
		z-index: 100;
		height: 100rpx;
		width: 100%;
		background-color: white;
		display: flex;
		justify-content: space-between;
	}

	.buy-button {
		height: 100rpx;
		width: 20%;
		background-color: #e89abe;
	}
</style>